<?php


class cart extends CI_Controller {



	function index()
	{
		
		 //echo "Welcome to orderdetail\n";
		 $data['title'] = "order Detail Page";
		 $this->load->view('cart', $data);
		 
	}
	
	function testCart()
	{		
		 //echo "Welcome to orderdetail\n";
		$this->load->model('cart_model');
		$cart = $this->cart_model->getCartInfo();
		$data['title'] = "order Detail Page";
		$data['cart'] = $cart;
		$this->load->view('restaurant/restaurantCart', $data);
		 
	}

	/**
	 * 
	 * 添加食物到购物车
	 * 参数放在request中，有foodID,quantity
	 */
	function addFood($foodID,$quantity)
	{
		$this->load->model('cart_model');
		$cart = $this->cart_model->addFood($foodID,$quantity);

		$data = array();
		$data['cart'] = $cart;
		$this->load->view('restaurant/restaurantCart', $data);
	}
	
	/**
	 * 
	 * 从购物车中减少食物
	 * 参数放在request中，有foodID,quantity
	 */
	function substractFood($foodID,$quantity)
	{
		/*
		if(!isset($_REQUEST['foodID'])
		|| !isset($_REQUEST['quantity']))
		{
			echo "addFood function 字段不完整";
			return;
		}
		
		$foodID = $_REQUEST['foodID'];
		$quantity = $_REQUEST['quantity'];
		*/
		$this->load->model('cart_model');
		$cart = $this->cart_model->substractFood($foodID,$quantity);
		$data = array();
		$data['cart'] = $cart;
		$this->load->view('restaurant/restaurantCart', $data);
	}
	
	/**
	 * 
	 * 从购物车中删除食物
	 * 参数放在request中，有foodID
	 */
	function removeFood($foodID)
	{
		/*
		if(!isset($_REQUEST['foodID']))
		{
			echo "removeFood function 字段不完整";
			return;
		}
		
		$foodID = $_REQUEST['foodID'];		
		*/
		$this->load->model('cart_model');
		$cart=$this->cart_model->removeFood($foodID);
		$data = array();
		$data['cart'] = $cart;
		$this->load->view('restaurant/restaurantCart', $data);
	}
	
	/**
	 * 
	 * 更新食物数量
	 * 参数放在request中，有foodID,quantity
	 */
	function updateFood()
	{
		//print_r($this->session->userdata('cart'));
		if(!isset($_REQUEST['foodID'])
		|| !isset($_REQUEST['quantity']))
		{
			echo "updateFood function 字段不完整";
			return;
		}
		
		$foodID = $_REQUEST['foodID'];
		$quantity = $_REQUEST['quantity'];
		
		$this->load->model('cart_model');
		$this->cart_model->updateFood($foodID,$quantity);
	}
	
	function clearCart()
	{
		$this->load->model('cart_model');
		$cart = $this->cart_model->clearCart();
	}
	/**
	 * 
	 * 请求当前购物车状态 
	 */
	function getCartInfo()
	{
		$this->load->model('cart_model');
		$cart = $this->cart_model->getCartInfo();
		echo "The Cart Info:<br/>";
		print_r($cart);
		echo "<br/>____________________<br/>";
		return $cart;
	}

}